<?php
if(isset($javascript)):
// load script in <head> section
echo $html->charset("UTF-8"); 
echo $javascript->link('scriptaculous/lib/prototype', false);
echo $javascript->link('scriptaculous/src/scriptaculous', false);
echo $javascript->link('scriptaculous/src/effects', false);
echo $javascript->link('scriptaculous/src/controls', false);
endif;
?>
<script>
function checkemail(emel){
    var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'checkemail'))?>/'+emel.value;
    var dajax=new Ajax.Request(url,{method:'post',onSuccess:doCheckEmail});
     $('UserName').value='';
     $('UserUsername').value='';
}

function doCheckEmail(response){
        dresponse=response.responseText;
        ddata=dresponse.toQueryParams();
        
        if(ddata.username.length>3) $('UserUsername').value=ddata.username;
        if(ddata.title_id) {
            for(var i=0;i<$('UserTitleId').length;i++){
                curopt=$('UserTitleId').options[i];
                if(curopt.value==ddata.title_id){
                    curopt.selected=true;
                }
            }
        }
        if(ddata.protocol_id) {
            for(var i=0;i<$('UserProtocolId').length;i++){
                curpid=$('UserProtocolId').options[i];
                if(curpid.value==ddata.protocol_id){
                    curpid.selected=true;
                }
            }
        }
        if(ddata.name.length>3) $('UserName').value=ddata.name;
        if(ddata.job_title.length>3) $('UserJobTitle').value=ddata.job_title;
        $('UserBahagian').value=ddata.bahagian;
        if(ddata.address.length>3) $('UserAddress').value=ddata.address;
        if(ddata.telephone.length>3) $('UserTelephone').value=ddata.telephone;
        if(ddata.fax.length>3) $('UserFax').value=ddata.fax;
        if(ddata.mobile.length>3) $('UserMobile').value=ddata.mobile;
}
function checkusername(username){
     var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'checkusername'))?>/'+username.value;
     var dajax=new Ajax.Request(url,{method:'post',onSuccess:doCheckUsername});
     $('UserName').value='';
     $('UserEmail').value='';
}

function doCheckUsername(response){
    dresponse=response.responseText;
    ddata=dresponse.toQueryParams();
   
    if($('UserUsername').value==ddata.username){
        
        if(ddata.email.length>3) $('UserEmail').value=ddata.email;
        if(ddata.title_id) {
            for(var i=0;i<$('UserTitleId').length;i++){
                curopt=$('UserTitleId').options[i];
                if(curopt.value==ddata.title_id){
                    curopt.selected=true;
                }
            }
        }
        if(ddata.scheme_id) {
            for(var i=0;i<$('UserSchemeId').length;i++){
                curpid=$('UserSchemeId').options[i];
                if(curpid.value==ddata.scheme_id){
                    curpid.selected=true;
                }
            }
        }
        if(ddata.protocol_id) {
            for(var i=0;i<$('UserProtocolId').length;i++){
                curpid=$('UserProtocolId').options[i];
                if(curpid.value==ddata.protocol_id){
                    curpid.selected=true;
                }
            }
        }
        if(ddata.name.length>3) $('UserName').value=ddata.name;
        if(ddata.job_title.length>3) $('UserJobTitle').value=ddata.job_title;
        $('UserBahagian').value=ddata.bahagian;
        if(ddata.address.length>3) $('UserAddress').value=ddata.address;
        if(ddata.telephone.length>3) $('UserTelephone').value=ddata.telephone;
        if(ddata.fax.length>3) $('UserFax').value=ddata.fax;
        if(ddata.mobile.length>3) $('UserMobile').value=ddata.mobile;
    }
}
function checkname(name){
	var enc_name=name.value.replace(/ /gi,'_');
	var enc_name=name.value.replace("/",'_slashes');
     var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'checkname'))?>/'+enc_name; //name.value;
     var dajax=new Ajax.Request(url,{method:'post',onSuccess:doCheckName});
     $('UserUsername').value='';
     $('UserEmail').value='';
}
function doCheckName(response){

    dresponse=response.responseText;
    ddata=dresponse.toQueryParams();

    if($('UserName').value==ddata.name){
        
        if(ddata.email.length>3) $('UserEmail').value=ddata.email;
        if(ddata.username.length>3) $('UserUsername').value=ddata.username;
        
        if(ddata.title_id) {
            for(var i=0;i<$('UserTitleId').length;i++){
                curopt=$('UserTitleId').options[i];
                if(curopt.value==ddata.title_id){
                    curopt.selected=true;
                }
            }
        }
        if(ddata.scheme_id) {
            for(var i=0;i<$('UserSchemeId').length;i++){
                curpid=$('UserSchemeId').options[i];
                if(curpid.value==ddata.scheme_id){
                    curpid.selected=true;
                }
            }
        }
        if(ddata.protocol_id) {
            for(var i=0;i<$('UserProtocolId').length;i++){
                curpid=$('UserProtocolId').options[i];
                if(curpid.value==ddata.protocol_id){
                    curpid.selected=true;
                }
            }
        }
       // if(ddata.name.length>3) $('UserName').value=ddata.name;
        $('UserUsername').value==ddata.username
        if(ddata.job_title.length>3) $('UserJobTitle').value=ddata.job_title;
        $('UserBahagian').value=ddata.bahagian;
        if(ddata.address.length>3) $('UserAddress').value=ddata.address;
        if(ddata.telephone.length>3) $('UserTelephone').value=ddata.telephone;
        if(ddata.fax.length>3) $('UserFax').value=ddata.fax;
        if(ddata.mobile.length>3) $('UserMobile').value=ddata.mobile;
    }
}
function clearall(){
	 $('UserName').disabled=false;
	 $('UserUsername').disabled=false;
	 $('UserEmail').disabled=false;
	 
     $('UserName').value='';
     $('UserUsername').value='';
     $('UserEmail').value='';
     $('UserTitleId').value='';
     $('UserSchemeId').value='';
     $('UserProtocolId').value='';
     $('UserJobTitle').value='';
     $('UserBahagian').value='';
     $('UserAddress').value='';
     $('UserTelephone').value='';
     $('UserFax').value='';
     $('UserMobile').value='';
}
</script>
<div class="memberships form">
<h2><?php __('Add Committee Member')?></h2>
<?php echo $form->create('Membership',array('url'=>array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'add')));?>
    <fieldset>
        <legend><?php __('Add Committee Member');?></legend>
        
        <div class="fieldset-inside">
        
        
<?php
echo $form->hidden('committee_id',array('value'=>$dcommittee['Committee']['id']));
echo "<span class='note'>".__('To search existing user, please key in the Name or Username or Email. Once the record found, press Tab key to automatically fill up all fields below.',true)."</span>";
echo $html->div('input',$form->label('Name').$ajax->autoComplete('User.name','/'.$dcommittee['Committee']['short_name'].'/memberships/getname',array('onKeydown'=>'checkname(this)'))); 
echo $html->div('input',$form->label('Username').$ajax->autoComplete('User.username','/'.$dcommittee['Committee']['short_name'].'/memberships/getusername',array('onKeydown'=>'checkusername(this)'))); 
echo $html->div('input',$form->label('Email').$ajax->autoComplete('User.email','/'.$dcommittee['Committee']['short_name'].'/memberships/getemail',array('onKeydown'=>'checkemail(this)'))); 
echo $form->button(__('Cancel', true),array('type'=>'button', 'onclick'=>'clearall()'));
echo $html->div('userdat',$form->inputs(
	array('role_id'=>array('value'=>2)
	,'User.title_id'=>array('options'=>$titles,'disabled'=>1)
	,'User.job_title'=>array('label'=>__('Post',true),'disabled'=>1)
	,'User.bahagian'=>array('label'=>__('Section/Division',true),'disabled'=>1)
	,'User.scheme_id'=>array('options'=>$schemes,'type' => 'select', 'empty' => __('--please select--',true),'disabled'=>1)
	,'User.protocol_id'=>array('options'=>$protocols,'label'=>__('Grade',true),'type' => 'select', 'empty' => __('--please select--',true),'disabled'=>1)
	,'User.address'=>array('disabled'=>1)
	,'User.telephone'=>array('disabled'=>1)
	,'User.fax'=>array('disabled'=>1)
	,'User.mobile'=>array('disabled'=>1))));
?>
        </div>
    </fieldset>
<?php echo $form->button(__('Save', true),array('type'=>'submit'));?>&nbsp;
<?php echo $form->button(__('Cancel', true), array('type'=>'button', 'onclick'=>'history.go(-1)'));?>
<?php echo $form->end();?>
</div>
